package com.example.controller;


import com.example.dto.SetmealDto;
import com.example.service.SetmealService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private UserService userService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 返回对应套餐的总数和套餐列表，确保每个smId只保存一次。
     * @param userId
     * @return
     */
    @GetMapping("/list")
    public Map<String, Object> listSetmealsByUserGender(@RequestParam("userId") String userId) {
        Integer sex = userService.getUserSexByUserId(userId);
        if (sex == null) {
            Map<String, Object> response = new HashMap<>();
            response.put("error", "User gender not found for userId: " + userId);
            return response;
        }

        List<SetmealDto> setmealDtoList = setmealService.getSetmealWithCheckItems(sex);
        Map<Integer, SetmealDto> uniqueSetmeals = new HashMap<>();
        for (SetmealDto setmealDto : setmealDtoList) {
            uniqueSetmeals.put(setmealDto.getSmId(), setmealDto);
        }

        int total = setmealService.getTotalSetmealCount(sex);
        Map<String, Object> response = new HashMap<>();
        response.put("total", total);
        response.put("data", uniqueSetmeals.values());

        return response;
    }

}
